package org.ihelpuoo.orm.handler;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.ihelpuoo.lang.ObjectUtils;
import org.ihelpuoo.orm.IIIException;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.filter.FieldFilter;

public abstract class SaveObjectFieldHandler extends FieldHandler
{

	public SaveObjectFieldHandler(ObjectProcessor beanHandler)
	{
		super(beanHandler);
	}

	@Override
	protected void processWithoutExceptionHandle(ObjectRule br, Field f, Object src, Object obj,
			FieldFilter actived, FieldFilter locked, String prefix) throws Exception
	{
		Method getter = ObjectUtils.getGetter(obj.getClass(),f);
		Object v = getter.invoke(obj,(Object[])null);
		if (v == null)
			return;
		Object subSrc = prepareSubSource(br,f,src);
		setObjectToSourceObject(processor.manual.getRule(v.getClass()),subSrc,v);
	}

	protected abstract void setObjectToSourceObject(ObjectRule br, Object src, Object obj)
			throws IIIException;

	protected abstract Object prepareSubSource(ObjectRule r, Field f, Object src) throws Exception;
}
